//
//  TiXianRecordListVC.m
//  NtoccShipper
//
//  Created by zcj on 2021/1/29.
//  Copyright © 2021 zhuchongjin. All rights reserved.
//

#import "TiXianRecordListVC.h"
#import "TiXianRecordListModel.h"
#import "TiXianRecordListCell.h"

@interface TiXianRecordListVC ()<UITableViewDelegate,UITableViewDataSource>{
    FlexFrameView *header;
}

@property (nonatomic,strong) TiXianRecordListCell *tiXianRecordListCell;
@property (nonatomic,strong) CJTableView *tableView;
@property (nonatomic,strong) NSMutableArray *modelArray;
@property (nonatomic,strong) NSString *refreshType; //刷新
@property (nonatomic,readwrite) int page;

@property (nonatomic,strong) CJLabel *labStartTime;
@property (nonatomic,strong) CJLabel *labEndTime;
@property (nonatomic,copy) NSString *strStartTime;
@property (nonatomic,copy) NSString *strEndTime;

@end

@implementation TiXianRecordListVC

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
//    DLog(@"self.indexPage======%@",self.indexPage);
    self.view.backgroundColor = [UIColor cjBackgroundColor];
    DLog(@"self.indexPage======%@",self.indexPage);

    self.tiXianRecordListCell = [[TiXianRecordListCell alloc]initWithFlex:nil reuseIdentifier:nil];
    [self createTopView];
    [self.view addSubview:self.tableView];
    WS(ws);
    self.tableView.placeHolderView = [[KKTableViewNoDataView alloc] initWithFrame:self.tableView.bounds image:[UIImage imageNamed:@"no_data"] viewClick:^{
           [ws.tableView.mj_header beginRefreshing];
    }];

    self.strStartTime = @"";
    self.strEndTime = @"";

    [self initData];
    [self loadData];
    [self setDefalutValue];
}

- (void)setDefalutValue{
    self.strStartTime = @"";
    self.strEndTime = @"";
    //    self.strSelectType = @"";
    self.labStartTime.text = @"开始时间";
    self.labEndTime.text = @"结束时间";
}

- (void)viewWillAppear:(BOOL)animated{
    DLog(@"self.indexPage======%@",self.indexPage);
    [self.tableView.mj_header beginRefreshing];
}
/// 创建tableview 等
- (void)createTopView{
    
    //    WS(ws);

    CGRect rcFrame = CGRectMake(0, 0, [[UIScreen mainScreen] bounds].size.width, 0);
    header = [[FlexFrameView alloc] initWithFlex:@"TiXianRecordTopView" Frame:rcFrame Owner:self];
    header.flexibleHeight = YES;
    [header layoutIfNeeded];

    
    [self.view addSubview:header];
    
}

- (void)initData{
    self.modelArray = [[NSMutableArray alloc]init];
    self.page = 1;
}

/// <#Description#>
- (void)loadData {

    NSString *strURL = CJStringWithFormat(@"%@/ntocc-tms-api/mybankWithdraw",CJBaseUrl);
    NSDictionary *dicPar = @{@"pageNum":CJStringWithFormat(@"%d",self.page),
                             @"pageSize":@"10",
                             @"status": self.indexPage,
                             @"gmtCreatedStart": self.strStartTime,
                             @"gmtCreatedEnd": self.strEndTime,
                            };

    WS(ws);
    [[HDNetworking sharedHDNetworking] GET:strURL parameters:dicPar success:^(id  _Nonnull responseObject) {

//        DLog(@"++++++++%@",responseObject);

        NSArray *list = responseObject[@"records"];
        if ([ws.refreshType isEqualToString:@"down"]) {
            [ws.modelArray removeAllObjects];
        }

        NSError * error ;
        NSArray *arr = [TiXianRecordListModel arrayOfModelsFromDictionaries:list error:&error];
        [ws.modelArray addObjectsFromArray:arr];

        [ws.tableView.mj_header endRefreshing];
        [ws.tableView.mj_footer endRefreshing];

        if (list.count < 10) {
            [ws.tableView.mj_footer endRefreshingWithNoMoreData];
        }


        [ws.tableView reloadData];
    } failure:^(NSError * _Nonnull error) {
        DLog(@"++++++++%@",error);
        [ws.tableView.mj_header endRefreshing];
        [ws.tableView.mj_footer endRefreshing];
    }];

}


- (void)downRefresh
{
    self.refreshType = @"down";
    self.page = 1;
    [self loadData];
}

- (void)upRefresh
{
    self.refreshType = @"up";
    self.page ++;
    [self loadData];
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return  self.modelArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *identifier = @"TiXianRecordListCell";
    TiXianRecordListCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
    if (cell == nil) {
        cell = [[TiXianRecordListCell alloc] initWithFlex:nil reuseIdentifier:identifier];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        
    }
    
    TiXianRecordListModel *model = self.modelArray[indexPath.row];
    [cell setCellInfoWithModel:model];

    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{

}


#pragma mark ----------btnActionAction

- (void)btnSelectStartTimeAction{
   
    [self btnShowDataTimeWithType:@"0"];
}
- (void)btnSelectEndimeAction{
    [self btnShowDataTimeWithType:@"1"];
}

- (void)btnShowDataTimeWithType:(NSString *)strType{
    
    WS(ws);
    BRDatePickerView *datePickerView = [[BRDatePickerView alloc]initWithPickerMode:BRDatePickerModeYMD];
    datePickerView.title = strType.intValue == 0 ? @"开始时间":@"截止时间";
    datePickerView.minDate = [NSDate br_setYear:2016 month:5 day:1];
    datePickerView.maxDate = [NSDate date];  //[NSDate br_setYear:2050 month:5 day:1];
    datePickerView.resultBlock = ^(NSString *selectValue) {

        if (strType.intValue == 0) {
            ws.labStartTime.text = selectValue;
            ws.strStartTime = CJStringWithFormat(@"%@",selectValue);
        } else {
            ws.labEndTime.text = selectValue;
            ws.strEndTime = CJStringWithFormat(@"%@",selectValue);
        }
        [ws.modelArray removeAllObjects];
        [ws.tableView.mj_header beginRefreshing];
    };
    
    BRPickerStyle *customStyle = [BRPickerStyle pickerStyleWithThemeColor:[UIColor cjMainColor]];
    datePickerView.pickerStyle = customStyle;
    [datePickerView show];

}

- (void)clearSelectTimeAction{
    [self setDefalutValue];
    [self.tableView.mj_header beginRefreshing];
}

- (void)pushVc:(UIViewController *)vc {
    
    if ([vc isKindOfClass:[UIViewController class]] == NO) return ;
    if (self.navigationController == nil) return ;
    if (vc.hidesBottomBarWhenPushed == NO) {
        vc.hidesBottomBarWhenPushed = YES;
    }
    
    [self.navigationController pushViewController:vc animated:YES];
}

- (CJTableView *)tableView{
    if (!_tableView) {
        _tableView = CJTableView.tbInitFrameStyle(CGRectMake(0, CJTableToTopHeight +header.bottom , SCREEN_WIDTH, SCREEN_HEIGHT-kTopHeight - kNavBarHeight -CJTableToTopHeight - header.height),UITableViewStylePlain)
        .tbDelegate(self)
        .tbDataSource(self)
        .tbSetOther(YES)
        .tbEstimatedRowHeight(YES)
        .tbMjHeadFooter(YES)
        ;
        WS(ws);
        _tableView.blockMjHeader = ^(BOOL isheader) {
            
            if (isheader) {
                [ws downRefresh];
            } else {
                [ws upRefresh];
            }

        };

    }
    
    return _tableView;
}

@end
